﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CoTuong
{
    class HighScore : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont myFont;

        Texture2D HighScoreTexture;
        Vector2 HighScore_Position;

        Texture2D btnBack;
        Vector2 btnBack_Position;

        Texture2D btnUpArrow;
        Vector2 btnUpArrow_Position;

        Texture2D btnDownArrow;
        Vector2 btnDownArrow_Position;

        bool isBackOnHover;
        bool isUpArrowInActive;
        bool isDownArrowInActive;
        int index;
        public static List<Player> ListPlayer = new List<Player>();

        public static bool BackToMenu = false;
        private readonly int widthSpan = 30;
        private readonly int heightSpan = 60;
        private readonly int maxShow = 5;
        
        public HighScore(Game game) : base(game)
        {
            index = 0;
            SortRanks();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);            
            myFont = Game.Content.Load<SpriteFont>(@"Font\myFont");
            HighScoreTexture = Game.Content.Load<Texture2D>(@"Images\bangxephang");
            btnBack = Game.Content.Load<Texture2D>(@"Images\Button\BackButton");
            btnUpArrow = Game.Content.Load<Texture2D>(@"Images\Button\UpArrow");
            btnDownArrow = Game.Content.Load<Texture2D>(@"Images\Button\DownArrow");

            HighScore_Position.X = Game.Window.ClientBounds.Width / 2 - HighScoreTexture.Width / 2;
            HighScore_Position.Y = Game.Window.ClientBounds.Height / 2 - HighScoreTexture.Height / 2;

            btnBack_Position.X = HighScore_Position.X + HighScoreTexture.Width / 2 - btnBack.Width / 2;
            btnBack_Position.Y = HighScore_Position.Y + HighScoreTexture.Height - btnBack.Height * 3;

            btnUpArrow_Position.X = HighScore_Position.X + HighScoreTexture.Width - 52;
            btnUpArrow_Position.Y = HighScore_Position.Y + HighScoreTexture.Height / 2 - 50;

            btnDownArrow_Position.X = HighScore_Position.X + HighScoreTexture.Width - 52;
            btnDownArrow_Position.Y = HighScore_Position.Y + HighScoreTexture.Height / 2 + 50;

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            try
            {
                #region Xử lý hơ chuột
                if (Game1.mousePosition.X > btnBack_Position.X && Game1.mousePosition.X < (btnBack_Position.X + btnBack.Width)
                    && Game1.mousePosition.Y > btnBack_Position.Y && Game1.mousePosition.Y < (btnBack_Position.Y + btnBack.Height))
                    isBackOnHover = true;
                else
                    isBackOnHover = false;
                #endregion

                #region Xử lý nút Back
                if (Game1.mouseClickedPosition.X > btnBack_Position.X && Game1.mouseClickedPosition.X < (btnBack_Position.X + btnBack.Width)
                    && Game1.mouseClickedPosition.Y > btnBack_Position.Y && Game1.mouseClickedPosition.Y < (btnBack_Position.Y + btnBack.Height))
                {
                    BackToMenu = true;
                }
                #endregion

                #region Xử lý nút mũi tên lên
                CheckActiveArrow();
                if (Game1.mouseClickedPosition.X > btnUpArrow_Position.X && Game1.mouseClickedPosition.X < (btnUpArrow_Position.X + btnUpArrow.Width)
                    && Game1.mouseClickedPosition.Y > btnUpArrow_Position.Y && Game1.mouseClickedPosition.Y < (btnUpArrow_Position.Y + btnUpArrow.Height)
                    && isUpArrowInActive)
                {
                    index--;
                }
                #endregion

                #region Xử lý nút mũi tên xuống
                if (Game1.mouseClickedPosition.X > btnDownArrow_Position.X && Game1.mouseClickedPosition.X < (btnDownArrow_Position.X + btnDownArrow.Width)
                    && Game1.mouseClickedPosition.Y > btnDownArrow_Position.Y && Game1.mouseClickedPosition.Y < (btnDownArrow_Position.Y + btnDownArrow.Height)
                    && isDownArrowInActive)
                {
                    index++;
                }
                #endregion

            }
            catch (Exception e)
            {
                Game1.errorMessage = e.Message;
            }
            base.Update(gameTime);
        }

        public static void WinUp(string name)
        {
            for(int i = 0; i < ListPlayer.Count; i++)
            {
                if (ListPlayer[i].Name == name)
                {
                    ListPlayer[i].Win++;
                    return;
                }
            }

            ListPlayer.Add(new Player(name, 1, 0));
        }

        public static void LoseUp(string name)
        {
            for (int i = 0; i < ListPlayer.Count; i++)
            {
                if (ListPlayer[i].Name == name)
                {
                    ListPlayer[i].Lose++;
                    return;
                }
            }

            ListPlayer.Add(new Player(name, 0, 1));
        }

        public override void Draw(GameTime gameTime)
        {
            Color myTransparentColor = new Color(255, 255, 255, 150);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            spriteBatch.Draw(HighScoreTexture, HighScore_Position, Color.White);

            //Vẽ nút Back
            if (isBackOnHover)
                spriteBatch.Draw(btnBack, btnBack_Position, Color.White);
            else
                spriteBatch.Draw(btnBack, btnBack_Position, myTransparentColor);

            //Vẽ nút Mũi tên lên, xuống
            if(isUpArrowInActive)
                spriteBatch.Draw(btnUpArrow, btnUpArrow_Position, Color.White);
            else
                spriteBatch.Draw(btnUpArrow, btnUpArrow_Position, myTransparentColor);

            if(isDownArrowInActive)
                spriteBatch.Draw(btnDownArrow, btnDownArrow_Position, Color.White);
            else
                spriteBatch.Draw(btnDownArrow, btnDownArrow_Position, myTransparentColor);

            spriteBatch.DrawString(myFont, "Name", new Vector2(HighScore_Position.X + widthSpan,
                HighScore_Position.Y + heightSpan), Color.Blue);
            spriteBatch.DrawString(myFont, "Win", new Vector2(HighScore_Position.X + widthSpan * 2 
                + myFont.MeasureString("Name  ").X, HighScore_Position.Y + heightSpan),Color.Blue);
            spriteBatch.DrawString(myFont, "Lose", new Vector2(HighScore_Position.X + widthSpan * 3
                            + myFont.MeasureString("Name Win").X, HighScore_Position.Y + heightSpan), Color.Blue);

            int ShowCount;
            if (ListPlayer.Count > maxShow)
                ShowCount = maxShow;
            else
                ShowCount = ListPlayer.Count;
            for (int i = 0; i < ShowCount; i++)
            {
                Vector2 posName;
                Vector2 posWin;
                Vector2 posLose;
                posName.X = HighScore_Position.X + widthSpan;
                posName.Y = posWin.Y = posLose.Y = HighScore_Position.Y + heightSpan * (i + 2);

                posWin.X = HighScore_Position.X + widthSpan + myFont.MeasureString("Name Win").X;
                posLose.X = HighScore_Position.X + widthSpan + myFont.MeasureString("Name Win Lose").X;

                spriteBatch.DrawString(myFont, ListPlayer[i + index].Name, posName, Color.Green);
                spriteBatch.DrawString(myFont, ListPlayer[i + index].Win.ToString(), posWin, Color.Green);
                spriteBatch.DrawString(myFont, ListPlayer[i + index].Lose.ToString(), posLose, Color.Red);
            }


            spriteBatch.End();
            base.Draw(gameTime);
        }


        private bool PlayerExists(string name)
        {
            foreach (Player p in ListPlayer)
            {
                if (p.Name.Equals(name))
                    return true;
            }
            return false;
        }

        private void SortRanks()
        {
            for (int i = 0; i < ListPlayer.Count; i++)
            {
                for (int j = i + 1; j < ListPlayer.Count; j++)
                {
                    if ((ListPlayer[i].Win - ListPlayer[i].Lose) <
                        ListPlayer[j].Win - ListPlayer[j].Lose)
                    {
                        Player ptemp = ListPlayer[i];
                        ListPlayer[i] = ListPlayer[j];
                        ListPlayer[j] = ptemp;
                    }
                }
            }
        }

        private void CheckActiveArrow()
        {
            if (ListPlayer.Count <= maxShow)
                isUpArrowInActive = isDownArrowInActive = false;
            else
            {
                if (index < ListPlayer.Count - maxShow)
                    isDownArrowInActive = true;
                else
                    isDownArrowInActive = false;

                if (index > 0)
                    isUpArrowInActive = true;
                else
                    isUpArrowInActive = false;
            }

        }
    }
}
